package cn.nn200433.mqtt.config;

import cn.nn200433.mqtt.core.HttpServer;
import cn.nn200433.mqtt.service.IEMQXAuthService;
import cn.nn200433.mqtt.service.impl.DefaultEMQXAuthServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * http服务器配置
 *
 * @author nn200433
 * @date 2021-07-23 03:39:49
 */
@Slf4j
@EnableConfigurationProperties(value = HttpServerProperties.class)
public class HttpServerAutoConfig {

    @Bean
    @ConditionalOnMissingBean
    public IEMQXAuthService emqxAuthService() {
        return new DefaultEMQXAuthServiceImpl();
    }

    @Bean
    @ConditionalOnProperty(prefix = HttpServerProperties.HTTP_SERVER_CONFIG_PREFIX, name = "enabled", havingValue = "true")
    public HttpServer httpServer(HttpServerProperties serverConfig, IEMQXAuthService emqxAuthService) {
        return new HttpServer(serverConfig.getPort(), emqxAuthService);
    }

}